package com.aim.Activity;


import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { // <1>
	private static final String TAG = "Preview";

	SurfaceHolder mHolder; // <2>
	public Camera camera; // <3>

	CameraPreview(Context context) {
		super(context);
		mHolder = getHolder(); // <4>
		mHolder.addCallback(this); // <5>
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>
	}

	// Called once the holder is ready
	public void surfaceCreated(SurfaceHolder holder) { // <7>
		

		try {
			camera = Camera.open(); // <8>
			camera.setPreviewDisplay(holder);
			camera.setPreviewCallback(new PreviewCallback() {
				public void onPreviewFrame(byte[] data, Camera camera) { // <11>
					CameraPreview.this.invalidate(); // <12>
				}
			});
		} catch (IOException e) { // <13>
			e.printStackTrace();
			Toast.makeText(getContext(), "Can't Open Camera", Toast.LENGTH_LONG);
		}
	}

	// Called when the holder is destroyed
	public void surfaceDestroyed(SurfaceHolder holder) { // <14>
		camera.setPreviewCallback(null);
		camera.stopPreview();
		camera.release();
	}

	// Called when holder has changed
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
		Camera.Parameters parameters = camera.getParameters();
		camera.startPreview();
	}

}